package com.qidian.mes.pro.service.impl;

import com.qidian.mes.pojo.ProcessStep;
import com.qidian.mes.pro.mapper.StepMapper;
import com.qidian.mes.pro.service.StepService;
import com.qidian.mes.util.dot.PagerDTO;
import com.qidian.mes.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author dqy
 * @Description
 * @Time 2024/4/9 15:46
 */
@Service
public class StepServiceImpl implements StepService {
    @Autowired
    private StepMapper stepMapper;
    @Override
    public QdResult saveStep(ProcessStep processStep) {
        if (processStep.getProcessStepCode() == null)
            return QdResult.error("编码不能为空");
        List<ProcessStep> processSteps = stepMapper.selectByCode(processStep.getProcessId(), processStep.getProcessStepCode());
        List<ProcessStep> processSteps1 = stepMapper.selectByProcessId(processStep.getProcessId());
        for (ProcessStep steps:processSteps1){
            System.out.println(steps);
        }
        if(processSteps != null && processSteps.size() > 0){
            for(ProcessStep steps:processSteps1){
                if (steps.getProcessStepCode().equals(processStep.getProcessStepCode())){
                    return QdResult.duplicate("该步骤编号已存在");
                }
            }
        }
        processSteps = stepMapper.selectByUrl(processStep.getProcessId(), processStep.getProcessStepUrl());
        if (processSteps != null && processSteps.size() > 0){
            for(ProcessStep steps:processSteps1){
                if (steps.getProcessStepUrl().equals(processStep.getProcessStepUrl())){
                    return QdResult.duplicate("该步骤url已存在");
                }
            }
        }
        return QdResult.isSuccess(stepMapper.insertStep(processStep));
    }

    @Override
    public QdResult updateStep(ProcessStep processStep) {
        return QdResult.isSuccess(stepMapper.updateStep(processStep));
    }

    @Override
    public QdResult deleteById(Long processStepId) {
        stepMapper.deleteById(processStepId);
        return QdResult.success();
    }

    @Override
    public QdResult deletes(Long[] processStepIds) {
        stepMapper.deleteByIds(processStepIds);
        return QdResult.success();
    }



    @Override
    public QdResult<ProcessStep> queryById(Long processStepId) {
        return QdResult.success(stepMapper.selectById(processStepId));
    }

    @Override
    public QdResult<PagerDTO<ProcessStep>> queryByPage(Long processId, int start, int limit) {
        PagerDTO<ProcessStep> pagerDTO = new PagerDTO<>();
        pagerDTO.setData(stepMapper.selectByPage(processId,(start-1)*limit,limit));
        pagerDTO.setPage(start);
        pagerDTO.setLimit(limit);
        pagerDTO.setTotal(stepMapper.selectCount(processId));
        return QdResult.success(pagerDTO);
    }
}
